package test3_tx.Advice;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import test3_tx.dao.AccountDao;
import test3_tx.domain.Account;

//aop
@Component
@Aspect
public class MyAdvice {

    @Autowired
    private AccountDao accountDao;

    @Pointcut("execution(void test3_tx.service.AccountService.transfer*(..))")
    private void pt_service(){};



    //在每次转账前后获取两个人各自拥有的钱数
    @Around("pt_service()")
    //这里需要抛出去异常，否则对开启了事务的方法，被这个方法加强之后，如果没被抛出，就没法回滚了
    private void around_advice(ProceedingJoinPoint pjp) throws Throwable {
        Account jerry = accountDao.getMoneyByName("Jerry");
        Account tom = accountDao.getMoneyByName("Tom");
        System.out.println(jerry);
        System.out.println(tom);
        try {
            System.out.println("transfer----");
            pjp.proceed();
        }finally {
            Account jerry2 = accountDao.getMoneyByName("Jerry");
            Account tom2 = accountDao.getMoneyByName("Tom");
            System.out.println(jerry2);
            System.out.println(tom2);
        }
    }

}
